import axios from 'axios';
import React from 'react';
import {Portal, Toast} from '@ant-design/react-native';
import Constants from '../common/Constants';
// axios 配置
axios.defaults.timeout = 6000;
axios.defaults.timeoutErrorMessage = '请求超时';
axios.defaults.retry = 4;

// const DEV_URL = 'http://139.159.227.130:9020/'; //DEV环境
// const UAT_URL = 'http://uat.apigateway.sieiot.com/'; //UAT环境

axios.defaults.baseURL = Constants.serverUrl;;
// axios.defaults.baseURL = UAT_URL;

axios.interceptors.request.use(
  config => {
    // 增加通用token

    if (global.xapp == undefined) {
      global.xapp = 'dev';
    }
    // console.log('global.URL', global.xapp);
    // // 切换环境时使用这句
    // config.baseURL = global.xapp == 'uat' ? UAT_URL : DEV_URL;

    // console.log('config', config);
    return config;
  },
  error => {
    return Promise.reject(error);
  },
);
// 返回状态判断
axios.interceptors.response.use(
  res => {
    // console.log('res', res);
    if (res.data.code === '-1') {
    //   ReduxService.getStore().dispatch({type: ISLOGIN, isLogin: false});
    //   NavigationService.resetRouter('Login');
      return Promise.reject({message: '登陆失效，请重新登陆', status: 'E'});
    }
    if (res.data.code === '805') {
      return Promise.reject({message: res.msg || res.data.msg});
    }
    return res; // 直接返回结果集里的data数据
  },
  err => {
    if (
      err.response.data.status === 500 ||
      err.response.data.message === '-1'
    ) {
    //   ReduxService.getStore().dispatch({type: ISLOGIN, isLogin: false});
    //   NavigationService.resetRouter('Login');
      return Promise.reject({message: '登陆失效，请重新登陆', status: 'E'});
    }
    // console.log('err', err);
    return Promise.reject(err);
  },
);

export default class HttpService extends React.Component {
  // 有菊花
  static post(url, params = {}, v) {
    const toastKey = Toast.loading('', 0);
    return new Promise<any>((resolve, reject) => {
      HttpService.postSimpleness(url, params, v)
        .then(res => {
          resolve(res);
          Portal.remove(toastKey);
        })
        .catch(error => {
          Portal.remove(toastKey);
          Toast.info(error.message);
          reject(error);
        });
    });
  }

  // 无菊花
  static postSimpleness(url, params = {}, v) {
    return new Promise<any>((resolve, reject) => {
      HttpService.postBasics(url, params, v)
        .then(res => {
          console.log("Res is:", res)
          resolve(res);
        })
        .catch(error => {
          console.log("error is:", error)
          reject(error);
        });
    });
  }

 static postBasics(url, params, v) {
    let headers = {};
    // headers.authorization = ReduxService.getState()
    //   ? ReduxService.getState().UserReducer.userToken
    //   : null;
    headers['Content-Type'] = 'application/json';
    if (url.indexOf('iot-base-attachment/single/file-upload') > -1) {
      headers['Content-Type'] = 'multipart/form-data;charset=UTF-8';
    } else {
      headers['Content-Type'] = 'application/json;charset=UTF-8';
    }
    // headers['refresh-authorization'] = ReduxService.getState()
    //   ? ReduxService.getState().UserReducer.refreshToken
    //   : null;

    // console.log(headers);
    return new Promise<any>((resolve, reject) => {
      axios
        .post(url, params, {headers: headers})
        .then(response => {
          resolve(response.data);
        })
        .catch(error => {
          reject(error);
        });
    }).catch(error => {
      return Promise.reject(error);
    });
  }

  // 有菊花
  static get(url, params = {}) {
    const toastKey = Toast.loading('', 0);
    return new Promise<any>((resolve, reject) => {
      HttpService.getBasics(url, params)
        .then(res => {
          resolve(res);
          Portal.remove(toastKey);
        })
        .catch(error => {
          Portal.remove(toastKey);
          Toast.info(error.message);
          reject(error);
        });
    });
  }

  // 无菊花
  static getSimpleness(url, params) {
    return new Promise<any>((resolve, reject) => {
      HttpService.getBasics(url, params)
        .then(res => {
          resolve(res);
        })
        .catch(error => {
          reject(error);
        });
    });
  }

 static getBasics(url, parameters) {
    // console.log(ReduxService.getState().user)
    return new Promise<any>((resolve, reject) => {
      axios({
        method: 'get',
        url: url,
        headers: {
          authorization: ReduxService.getState()
            ? ReduxService.getState().UserReducer.userToken
            : null,
          'refresh-authorization': ReduxService.getState()
            ? ReduxService.getState().UserReducer.refreshToken
            : null,
        },
        params: params,
      })
        .then(res => {
          resolve(res.data);
        })
        .catch(error => {
          reject(error);
        });
    });
  }
}
